package spring.cloud.chapter2.controller;

//import io.github.yedaxia.apidocs.ApiDoc;
import io.swagger.annotations.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import spring.cloud.chapter2.pojo.Account;
import spring.cloud.chapter2.utils.ResponseUtils;
import spring.cloud.chapter2.vo.ResultMessage;

/**
 * 账户接口
 */
@Api(tags = "账户接口")
@Controller
@RequestMapping("/fund")
public class AccountController {
//result: 这个可以直接声明返回的对象类型，如果你声明了，将会覆盖SpringBoot的返回对象
//url: 请求URL，扩展字段，用于支持非SpringBoot项目
//method: 请求方法，扩展字段，用于支持非SpringBoot项目
//@ApiDoc(result = AdminVO.class, url = "/api/v1/admin/login2", method = "post")
    /**
     * 账户页面
     * @return
     */
    @ApiOperation(value="账户页面访问",notes = "无参数")
    @GetMapping("/account/page")
    public String page(){
        return "account";
    }

    /**
     * 获取账户
     * @param id 账户ID
     * @return
     */
    @ApiOperation(value="获取账户",notes = "根据ID获取账户",consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(paramType = "path",name = "id",value = "用户ID",required = true,dataType = "long",example = "1")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 401,message = "需要认证"),
            @ApiResponse(code = 403,message = "请求被拒绝"),
            @ApiResponse(code = 404,message = "没有这个请求地址"),
            @ApiResponse(code = 500,message = "服务器内部错误")
    })
    @GetMapping(value = "/account/{id}",consumes="application/json", produces="application/json")
    @ResponseBody
    public Account getAccount(@PathVariable(value = "id",required = true) Long id){

        Account account = new Account();
        account.setId(id);
        account.setAccountName("account_"+id);
        double balance = id % 10 * 10000.0* Math.random();
        account.setBalance(balance);
        account.setNote("note_"+id);
        return account;
    }

    /**
     * 创建账户
     * @param account 账户对象
     * @return
     */
    @ApiOperation(value = "创建账户",notes = "创建用户账户",consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "账户对象",required = true,dataType = "Account",paramType = "body")
    })
    @ApiResponses({
            @ApiResponse(code=200,message = "请求成功"),
            @ApiResponse(code=201,message = "创建成功"),
            @ApiResponse(code = 401,message = "需要认证"),
            @ApiResponse(code = 403,message = "请求被拒绝"),
            @ApiResponse(code = 404,message = "没有这个请求地址"),
            @ApiResponse(code = 500,message = "服务器内部错误")
    })
    @PostMapping("/account")
    @ResponseBody
    public Account createAccount(@RequestBody Account account){
        long id = (long) (10000.0 * Math.random());
        account.setId(id);
        return account;
    }

    /**
     * 更新账户
     * @param account 需要更新的账户对象
     * @return
     */
    @ApiOperation(value = "更新账户",notes = "更新用户账户")
    @ApiImplicitParam(name = "account",value = "用户账户对象",required = true,dataType = "Account",paramType = "body")
    @ApiResponses({
            @ApiResponse(code=200,message = "更新成功"),
            @ApiResponse(code = 401,message = "需要认证"),
            @ApiResponse(code = 403,message = "请求被拒绝"),
            @ApiResponse(code = 404,message = "没有这个请求地址"),
            @ApiResponse(code = 500,message = "服务器内部错误")
    })
    @PutMapping("/account")
    @ResponseBody
    public ResultMessage updateAccount(@RequestBody Account account){

        System.out.println("更新账户");
        return new ResultMessage(true,"更新账户成功");
    }

    /**
     * 删除账户
     * @param id 账户的ID
     * @return
     */
    @ApiOperation(value = "删除账户",notes = "要删除的账户ID")
    @ApiImplicitParam(name = "id",value = "账户ID",required = true,dataType = "Long",paramType = "path",example = "1")
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除成功"),
            @ApiResponse(code = 401,message = "需要认证"),
            @ApiResponse(code = 403,message = "请求被拒绝"),
            @ApiResponse(code = 404,message = "没有这个请求地址"),
            @ApiResponse(code = 500,message = "服务器内部错误")
    })
    @DeleteMapping("/account/{id}")
    @ResponseBody
    public ResultMessage deleteAccount(@PathVariable("id") Long id){
        System.out.println("删除账户");
        return new ResultMessage(true,"删除账户成功");
    }

    /**
     * 创建账户2
     * @param account 账户对象
     * @return
     */
//    @ApiDoc(result = String.class)
    @ApiOperation(value = "创建账户2",notes = "创建账户的第二个接口")
    @ApiImplicitParam(name = "account",value = "账户对象",required = true,dataType = "Account",paramType = "body")
    @ApiResponses({
            @ApiResponse(code = 200,message = "创建成功"),
            @ApiResponse(code = 401,message = "需要认证"),
            @ApiResponse(code = 403,message = "请求被拒绝"),
            @ApiResponse(code = 404,message = "没有这个请求地址"),
            @ApiResponse(code = 500,message = "服务器内部错误")
    })
    @PostMapping("/account2")
    @ResponseBody
    public ResponseEntity<Account> createAccount2(@RequestBody Account account){

        ResponseEntity<Account> response = null;
        HttpStatus status = null;
        HttpHeaders headers = new HttpHeaders();
        boolean exFlag = false;
        try{
            long id = (long)(10000.0* Math.random());
            account.setId(id);
            throw new RuntimeException();
        }catch (Exception e){
            exFlag = true;
        }

        return exFlag ? ResponseUtils.generateResponseEntity(account,HttpStatus.OK,false,"create account error,plz check ur input!!"):
                ResponseUtils.generateResponseEntity(account,HttpStatus.CREATED,true,"create account success!!");
    }
}
